package com.lzp.service.impl;

import com.lzp.mapper.CarouselMapper;
import com.lzp.mapper.CategoryMapper;
import com.lzp.mapper.CategoryMapperCustom;
import com.lzp.pojo.Carousel;
import com.lzp.pojo.Category;
import com.lzp.pojo.vo.CategoryVO;
import com.lzp.pojo.vo.NewItemsVO;
import com.lzp.service.CarouselService;
import com.lzp.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类业务实现接口
 *
 * @author dtyy
 * @date 2020/8/2
 */
@Service
public class CategorylServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryMapperCustom categoryMapperCustom;

    /**
     * 查询所有一级分类
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> listRootCategories() {

        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", 1);

        List<Category> result = categoryMapper.selectByExample(example);

        return result;
    }

    /**
     * 根据一级分类查询子分类信息
     *
     * @param rootCatId 一级分类id
     * @return
     */
    @Override
    public List<CategoryVO> listSubCategories(Integer rootCatId) {
        return categoryMapperCustom.listSubCategories(rootCatId);
    }

    /**
     * 首页查询每个一级分类下的6条最新商品数据
     *
     * @param rootCatId 一级分类id
     * @return
     */
    @Override
    public List<NewItemsVO> listSixNewItems(Integer rootCatId) {
        // 区别于listSubCategories，采用map
        Map<String, Object> params = new HashMap<>();
        params.put("rootCatId", rootCatId);
        return categoryMapperCustom.listSixNewItems(params);
    }

}
